/**
 * All rights reserved by DigitNexus Technology INC.
 */

package com.xspace.android.xafcommon.appliction;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;

import com.xspace.android.imcommon.interfaces.ImServiceConnection;
import com.xspace.android.xafcommon.db.AssetsDatabaseManager;
import com.xspace.android.xafcommon.util.BaseRProxy;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.ImageUtils;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.PreferencesUtils;
import com.xspace.android.xafcommon.util.StringUtils;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;


/**
 * @version V1.0.0
 * @ClassName: BaseApplication
 * @author:qbluo
 * @Date：2014-2-21
 * @Description: TODO
 */
public class BaseApplication extends Application {

    public static final String TAG = BaseApplication.class.getSimpleName();

    protected static Context mContext;
    public static BaseApplication mInstance;

    protected boolean mIsAccountConfigured;
    protected boolean mIsAccountLogined;
    private final PreferenceListener mPreferenceListener = new PreferenceListener();
    //    private MyLocation mMyLocation;
    private static Handler mHandler;
    public static OkHttpClient sOkHttpClient;
    static final int DISK_CACHE_SIZE = 50 * 1024 * 1024; // 50MB

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        mInstance = this;
        mHandler = new Handler(getMainLooper());
        String login = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ID);
        String password = PreferencesUtils.getPfValue(Constants.ACCOUNT_PASSWORD_KEY);
        String isVal = PreferencesUtils.getPfValue(Constants.ACCOUNT_VALIDATED);
        mIsAccountConfigured = !StringUtils.isEmpty(login) && (!StringUtils.isEmpty((password))) && "1".equals(isVal);
        initPushListener();

        ImageUtils.init(mInstance);
        AssetsDatabaseManager.initManager(this);
//        TCAgent.LOG_ON = true;
        // App ID: 在TalkingData创建应用后，进入数据报表页中，在“系统设置”-“编辑应用”页面里查看App ID。
        // 渠道 ID: 是渠道标识符，可通过不同渠道单独追踪数据。
//        TCAgent.init(this, "5F062704DC90587C4F4F114B9BFD5F09", "td_wmh_android");
        File cacheDir = new File(BaseApplication.mInstance.getCacheDir(), "http");
        Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
        sOkHttpClient = new OkHttpClient()
                .newBuilder()
                .connectTimeout(1, TimeUnit.MINUTES) //设置连接超时
                .readTimeout(1, TimeUnit.MINUTES) //设置读取超时
                .writeTimeout(1, TimeUnit.MINUTES) //设置写入超时
                .cache(cache)
                .build();
    }

    public void initPushListener() {
        try {
            LogUtil.d(TAG, "wubaojie>>>initPushListener: " + BaseRProxy.IM_SERVICE_NAME);
            ImServiceConnection.init(BaseApplication.mInstance, BaseRProxy.IM_SERVICE_NAME);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Handler getMainHandler() {
        return mHandler;
    }

    /**
     * @param @return
     * @return
     * @throws
     * @Description: 判断是否配置文件已配置
     */
    public boolean isAccountConfigured() {
        String login = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ID);
        String password = PreferencesUtils.getPfValue(Constants.ACCOUNT_PASSWORD_KEY);
        String isVal = PreferencesUtils.getPfValue(Constants.ACCOUNT_VALIDATED);
        mIsAccountConfigured = !StringUtils.isEmpty(login) && (!StringUtils.isEmpty((password))) && "1".equals(isVal);
        return mIsAccountConfigured;
    }

//    public boolean isAccountLogined() {
//        String login = (String) PreferencesUtils.getPfValue(this, Constants.ACCOUNT_BAREJID_KEY,
//                String.class.getName());
//        String password = (String) PreferencesUtils.getPfValue(this, Constants.ACCOUNT_PASSWORD_KEY,
//                String.class.getName());
//
////        String isVal = (String) PreferencesUtils.getPfValue(this, Constants.ACCOUNT_VALIDATED, String.class.getName
//// ());
//        mIsAccountLogined = !StringUtils.isEmpty(login) && (!StringUtils.isEmpty((password)));
//        return mIsAccountLogined;
//    }

    private class PreferenceListener implements SharedPreferences.OnSharedPreferenceChangeListener {

        public PreferenceListener() {
        }

        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            System.out.println("===========onSharedPreferenceChanged===========" + key);
            if (Constants.ACCOUNT_BAREJID_KEY.equals(key) || Constants.ACCOUNT_PASSWORD_KEY.equals(key)
                    || Constants.ACCOUNT_VALIDATED.equals(key)) {
                String login = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ID);
                String password = PreferencesUtils.getPfValue(Constants.ACCOUNT_PASSWORD_KEY);
                String isVal = PreferencesUtils.getPfValue(Constants.ACCOUNT_VALIDATED);
                mIsAccountConfigured = !StringUtils.isEmpty(login) && (!StringUtils.isEmpty((password))) && "1"
                        .equals(isVal);
            }
        }
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        LogUtil.v(TAG, "wubaojie>>>onTerminate>>>>>");
        PreferencesUtils.handlerOnSharedPreferenceChangeListener(this, mPreferenceListener, false);
    }
}
